import { ref, customRef } from 'vue'

export default function (initValue: string, delay: number) {
  let timer: number
  // track(跟踪), trigger(触发)
  let message = customRef((track, trigger) => {
    return {
      get() {
        console.log('customRef get :>> ');
        track() // 告诉Vue持续关注，类似订阅
        return initValue
      },
      set(value) {
        console.log('customRef set :>> ', value);
        clearTimeout(timer)
        timer = setTimeout(() => {
          initValue = value
          trigger() // 通知Vue数据发生变化，类似发布
        }, 1000)
      }
    }
  })
  return { message }
}

